package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
     @Autowired
     private RedisTemplate redisTemplate;
    /**
     *
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("添加菜品")
    public Result save(@RequestBody DishDTO dishDTO)
    {
        log.info("开始菜品添加{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);
        //添加成功之后，将redis缓存进行删除
        String key="dish_"+dishDTO.getCategoryId();
        redisTemplate.delete(key);
        return Result.success();
    }
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> QueryPage(DishPageQueryDTO dishPageQueryDTO)
    {
        log.info("开始菜品分类查询{}",dishPageQueryDTO);
       PageResult pageResult=dishService.QueryPage(dishPageQueryDTO);

        return Result.success(pageResult);
    }
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result deleteBatch(@RequestParam List<Long> ids)
    {
        log.info("菜品批量删除操作{}",ids);
        dishService.deleteBatch(ids);
        //将所有的菜品缓存数据清理掉，所有以dish_开头的
        Set keys = redisTemplate.keys("dish_");
        redisTemplate.delete(keys);
        return Result.success();
    }

}
